package cricket;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class CricketGame extends JFrame implements ActionListener{

	public static Integer[] targets = new Integer[] {20,19,18,17,16,15, 25};
	private Vector <CricketPlayer> players = new Vector<CricketPlayer>();
	
	private Vector<JLabel> scoreLabels = new Vector<JLabel>();
	private Vector<JLabel> hitLabelsP1 = new Vector<JLabel>();
	private Vector<JLabel> hitLabelsP2 = new Vector<JLabel>();
	
	public CricketGame() {
		
		players.add(new CricketPlayer("Justin"));
		players.add(new CricketPlayer("Jeff"));
		
		scoreLabels.add(new JLabel("0"));
		scoreLabels.add(new JLabel("0"));
		
		for (int i = 0; i < targets.length; i++) {
			JLabel l1 = new JLabel("");
			l1.setOpaque(true);
			hitLabelsP1.add(l1);
			JLabel l2 = new JLabel("");
			l2.setOpaque(true);
			hitLabelsP2.add(l2);
		}
		
		createGUI();
		this.setVisible(true);
	}


	private void createGUI() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(new Dimension(500,800));
		this.setLayout(new GridLayout(0,5));
		this.add(new JLabel(players.get(0).getName()));
		this.add(new JLabel(""));
		this.add(new JLabel("|"));
		this.add(new JLabel(""));
		this.add(new JLabel(players.get(1).getName()));
		
		for (int i = 0; i < targets.length; i ++) {
			JButton b1 = new JButton(players.get(0).getName() + "," + targets[i]);
			b1.addActionListener(this);
			this.add(b1);
			this.add(hitLabelsP1.get(i));
			this.add(new JLabel(targets[i] + ""));
			this.add(hitLabelsP2.get(i));
			JButton b2 = new JButton(players.get(1).getName() + "," + targets[i]);
			b2.addActionListener(this);
			this.add(b2);
		}
		
		this.add(scoreLabels.get(0));
		this.add(new JLabel(""));
		this.add(new JLabel("|"));
		this.add(new JLabel(""));
		this.add(scoreLabels.get(1));
	}
	
	public static void main(String[] args) {
		new CricketGame();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		String[] args = ((JButton) e.getSource()).getText().split(",");
		String name = args[0];
		Integer num = Integer.parseInt(args[1]);
		
		CricketPlayer activePlayer = null;
		boolean canScore = false;
		for (CricketPlayer player : players) {
			if (player.getName().equals(name)) {
				activePlayer = player;
			} else {
				if (player.getScores().get(num) < 3) {
					canScore = true;
				}
			}
		}
		
		activePlayer.hit(num, canScore);
		
		//update GUI
		for (int i = 0; i < targets.length; i++) {
			if (players.get(0).getScores().get(targets[i]) > 2) { 
				hitLabelsP1.get(i).setBackground(Color.red);
			}
			if (players.get(1).getScores().get(targets[i]) > 2) { 
				hitLabelsP2.get(i).setBackground(Color.blue);
			}
			hitLabelsP1.get(i).setText(players.get(0).getScores().get(targets[i]) + "");
			hitLabelsP2.get(i).setText(players.get(1).getScores().get(targets[i]) + "");
		}
		
		scoreLabels.get(0).setText(players.get(0).getScore() +"");
		scoreLabels.get(1).setText(players.get(1).getScore() +"");
	}
	
}
